﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Caching;
using MySQL.Db;
using Gollie.WebCache;

namespace Gollie.DataTables
{
	public class ActiveOffersQuickViewItem
	{
		public uint VendorId { get; set; }
		public string Bedrijfsnaam { get; set; }
		public string Straat { get; set; }
		public string Plaats { get; set; }
		public string Website { get; set; }
		public long Count { get; set; }
	}

	public class ActiveOffersQuickView : BaseBindable<ActiveOffersQuickViewItem>, ICacheable
	{
		private static string cacheName = "activeoffersquickview";

		public ActiveOffersQuickView()
		{
			Policy = CacheDependencyBase.CreatePolicy(300, new string[] { CacheConfig.OffersChanged });

			ResultSet resultSet = Database.Queries.GetNumberOfOffersPerVendor();

			this.AddDetails(resultSet);
		}

		private void AddDetails(ResultSet resultSet)
		{
			int numRows = resultSet.Count;

			for (int i = 0; i < numRows; i++)
			{
				this.Add(new ActiveOffersQuickViewItem() {
					VendorId = resultSet.GetUInt32(i, "id"),
					Bedrijfsnaam = resultSet.GetString(i, "name"),
					Count = resultSet.GetInt64(i, "numberoffers"),
					Plaats = resultSet.GetString(i, "city"),
					Straat = resultSet.GetString(i, "street"),
					Website = resultSet.GetString(i, "website") 
				});
			}
		}

		internal static ActiveOffersQuickView Instance
		{
			get
			{
				return DataCache.GetCacheableObject<ActiveOffersQuickView>(cacheName);
			}
		}

#region ICacheable
		public CacheItemPolicy Policy { get; set; }
		public void Initialize(params object[] args) { }
#endregion

		public static void RemoveFromCache()
		{
			DataCache.Cache.Remove(cacheName);
		}
	}
}